//
//  MainTabBarController.m
//  BaiTuan
//
//  Created by mycomputer on 15/12/25.
//  Copyright © 2015年 YR. All rights reserved.
//

#import "MainTabBarController.h"
#import "Common.h"
#import "HWTabBarItem.h"
@interface MainTabBarController ()

@end

@implementation MainTabBarController
{
    UIImageView *_tabBgImageView;
    NSMutableArray *_btmArray;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self _createSubviewController];
    [self _customTabBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)_customTabBar{
    //移除tabBar 上的所有按钮
    for (UIView *view in self.tabBar.subviews) {
        Class cls = NSClassFromString(@"UITabBarButton");
        if ([view isKindOfClass:cls]) {
            [view removeFromSuperview];
        }
    }
    
    
    //tabBar 背景图片设置
    _tabBgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -6, kScreenWidth, 55)];
    _tabBgImageView.backgroundColor = [UIColor whiteColor];
    [self.tabBar addSubview:_tabBgImageView];
    
    _btmArray = [NSMutableArray arrayWithCapacity:4];
    
    NSArray *imageNames = @[@"tabbar11.png",@"tabbar12.png",@"tabbar13.png",@"tabbar14.png"];
//    NSArray *imageNamesSd = @[@"tabbar21.png",@"tabbar22.png",@"tabbar23.png",@"tabbar24.png"];
    NSArray *titles = @[@"首页",@"附近",@"我的",@"更多"];
    CGFloat buttonWidth = (CGFloat)kScreenWidth/imageNames.count;
    for (int i = 0 ; i < imageNames.count; i++) {
        NSString *title = titles[i];
        NSString *name = imageNames[i];
//        NSString *nameSd =imageNamesSd[i];
        CGRect frame = CGRectMake(i*buttonWidth, 0, buttonWidth, 49);
        HWTabBarItem *button = [[HWTabBarItem alloc] initWithFrame:frame imageName:name title:title];
        button.tag = i;
        [button addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
        _btmArray[i] = button;
        [self.tabBar addSubview:button];
        if (button.tag == 0) {
            NSString *name = @"tabbar21.png";
            [button setSelected:YES sdName:name name:nil];
        }
    }
    
    
}

- (void)_createSubviewController{
    
    NSArray  *names =  @[@"Home",@"Nearby",@"Profile",@"More"];
//    NSArray *chsNames = @[@"团购",@"附近",@"个人",@"更多"];
    
    NSMutableArray *navArray = [[NSMutableArray alloc] init];
    
//    int i = 0;
    for (NSString *name in names) {
        
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:name bundle:nil];
        UINavigationController *nav = [storyBoard instantiateInitialViewController];
//        nav.topViewController.title = chsNames[i++];
        [navArray addObject:nav];
        
    }
    self.viewControllers = navArray;
    
}

//底部button点击事件，切换视图
- (void)buttonSelected:(HWTabBarItem *)button{
    NSInteger i = 0;
    self.selectedIndex = button.tag;
    NSArray *imageNames = @[@"tabbar11.png",@"tabbar12.png",@"tabbar13.png",@"tabbar14.png"];
    NSString *name = imageNames[self.selectedIndex];
    NSArray *imageNamesSd = @[@"tabbar21.png",@"tabbar22.png",@"tabbar23.png",@"tabbar24.png"];
    
    HWTabBarItem *btn = _btmArray[self.selectedIndex];
    NSString *nameSd =imageNamesSd[self.selectedIndex];
    NSArray *array = self.tabBar.subviews;
    for (UIView *view in array) {
        if ([view isKindOfClass:[HWTabBarItem class]]) {
            HWTabBarItem *btn = (HWTabBarItem *)view;
            NSString *name = imageNames[i];
            i++;
            [btn setSelected:NO sdName:nameSd name:name];
        }
    }
    [btn setSelected:YES sdName:nameSd name:name];
}

//- (void)setSelectedIndex:(NSUInteger)selectedIndex
//{
//    [super setSelectedIndex:selectedIndex];
//   
//    HWTabBarItem *btn = _btmArray[selectedIndex];
//    btn.selected = YES;
//    NSArray *imageNamesSd = @[@"tabbar21.png",@"tabbar22.png",@"tabbar23.png",@"tabbar24.png"];
//    
//    NSString *nameSd =imageNamesSd[selectedIndex];
//    NSArray *imageNames = @[@"tabbar11.png",@"tabbar12.png",@"tabbar13.png",@"tabbar14.png"];
//    NSString *name = imageNames[self.selectedIndex];
//    [btn setSelected:YES sdName:nameSd name:name];
//    
//
//}
//
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
